package it.orma.iridehr.service.jobs;

import it.orma.iridehr.controllers.form.admin.EmailJobForm;
import it.orma.iridehr.entities.Parametro;
import it.orma.iridehr.jobs.InitializingCronTrigger;
import it.orma.iridehr.repository.ParametriRepository;

import org.quartz.JobDataMap;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

public class EmailSchedulerService {

	@Autowired
	SchedulerFactoryBean schedulerFactoryBean;
	
	@Autowired
	ParametriRepository parametriRepository;
	
	public Boolean stopEmailJob(EmailJobForm eForm) throws Exception{
				
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		InitializingCronTrigger trigger = (InitializingCronTrigger) scheduler.getTrigger ("cronTrigger", Scheduler.DEFAULT_GROUP);
		trigger.setCronExpression("59 59 23 31 12 ? 2099");
		scheduler.rescheduleJob ("cronTrigger", Scheduler.DEFAULT_GROUP, trigger);
		Parametro param1 = new Parametro();
		param1.setChiave("EMAIL_CRON_EXPRESSION");
		param1.setValore("59 59 23 31 12 ? 2099");
		parametriRepository.salvaParametro(param1);	
		loadForm(eForm);
		return true;

	}
	
	public void loadForm(EmailJobForm eForm){
		String cronExpression = parametriRepository.ricercaParametro("EMAIL_CRON_EXPRESSION");
		String messageWindow = parametriRepository.ricercaParametro("EMAIL_MESSAGE_WINDOW");
		String[] times = cronExpression.split(" ");
		Long pollingInterval = null;
		if (!cronExpression.equals("59 59 23 31 12 ? 2099")){
			int everySec = times[0].indexOf("/");
			if (everySec>-1){
				pollingInterval = Long.parseLong(times[0].substring(times[0].indexOf("/")+1));
			}else{
				int everyMin = times[1].indexOf("/");
				if (everyMin>-1){
					pollingInterval = Long.parseLong(times[1].substring(times[1].indexOf("/")+1))*60+
							          Long.parseLong(times[0]);
				}else{
					int everyHour = times[2].indexOf("/");
					if (everyHour>-1){
						pollingInterval = Long.parseLong(times[2].substring(times[2].indexOf("/")+1))*60*60+
								          Long.parseLong(times[1])*60+Long.parseLong(times[0]);
					}
				}
			}
		}
		eForm.setDelay(0l);
		eForm.setMessageWindow(Integer.parseInt(messageWindow));
		eForm.setPollingInterval(pollingInterval);
	}
	
	public Boolean scheduleEmailJob(EmailJobForm ejForm) throws Exception{
		
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		InitializingCronTrigger trigger = (InitializingCronTrigger) scheduler.getTrigger ("cronTrigger", Scheduler.DEFAULT_GROUP);
		
		Long time = ejForm.getPollingInterval();
		int secs = (int) (time%60);
		int minutes = (int) ((time/60)%60);
		int hours = (int)(time/3600);
		String cronExpression = null;
		if(hours==0&&minutes==0){
			cronExpression = "0/"+secs+" * * * * ?";
			trigger.setStartDelay(secs*1000);
		}else if (hours==0&&minutes!=0){
			cronExpression = secs+" 0/"+minutes+" * * * ?";
			trigger.setStartDelay(secs*1000+minutes*60000);
		}else if (hours!=0){
			cronExpression = secs+" "+minutes+" 0/"+hours+" * * ?";
			trigger.setStartDelay(secs*1000+minutes*60000+hours*60*60*1000);
		}
		Parametro param = new Parametro();
		param.setChiave("EMAIL_CRON_EXPRESSION");
		param.setValore(cronExpression);
		parametriRepository.salvaParametro(param);	
		param.setChiave("EMAIL_MESSAGE_WINDOW");
		param.setValore(String.valueOf(ejForm.getMessageWindow()));
		parametriRepository.salvaParametro(param);	
		
		JobDataMap jdm = new JobDataMap();
		jdm.put("messageWindow", ejForm.getMessageWindow());
		trigger.setJobDataMap(jdm);
		trigger.setCronExpression(cronExpression);
		scheduler.rescheduleJob ("cronTrigger", Scheduler.DEFAULT_GROUP, trigger);
		loadForm(ejForm);
		return true;
	}
	
}
